// CAN controller commands
#define MCP_RESET	 		0b11000000
#define MCP_READ	 		0b00000011
#define MCP_WRITE			0b00000010
#define MCP_RTS 			0b10000000
#define MCP_READ_STATUS 	0b10100000
#define MCP_BIT_MODIFY		0b00000101
#define MCP_RX_STATUS       0b10110000


// Control registers in the CAN controller
#define MCP_CANCTRL 0x0F
#define MCP_CANSTAT 0x0E
#define MCP_CANINTE 0x2B
#define MCP_CANINTF 0x2C

#define CAN_INTE		0x2B
#define CAN_INTF		0x2C
#define CAN_BFPCTRL		0x0C
#define CAN_TXRTSCTRL	0x0D
#define CAN_TXB0CTRL	0x30
#define CAN_TXB1CTRL	0x40
#define CAN_TXB2CTRL	0x50
#define CAN_TXB0SIDH	0x31
#define CAN_TXB1SIDH	0x41
#define CAN_TXB2SIDH	0x51
#define CAN_TXB0SIDL	0x32
#define CAN_TXB1SIDL	0x42
#define CAN_TXB2SIDL	0x52
#define CAN_TXB0EID8	0x33
#define CAN_TXB1EID8	0x43
#define CAN_TXB2EID8	0x53
#define CAN_TXB0EID0	0x34
#define CAN_TXB1EID0	0x44
#define CAN_TXB2EID0	0x54
#define CAN_TXB0DLC		0x35
#define CAN_TXB1DLC		0x45
#define CAN_TXB2DLC		0x55
#define CAN_TXB0D0		0x36
#define CAN_TXB0D1		0x37
#define CAN_TXB0D2		0x38
#define CAN_TXB0D3		0x39
#define CAN_TXB0D4		0x3A
#define CAN_TXB0D5		0x3B
#define CAN_TXB0D6		0x3C
#define CAN_TXB0D7		0x3D
#define CAN_TXB1D0		0x46
#define CAN_TXB1D1		0x47
#define CAN_TXB1D2		0x48
#define CAN_TXB1D3		0x49
#define CAN_TXB1D4		0x4A
#define CAN_TXB1D5		0x4B
#define CAN_TXB1D6		0x4C
#define CAN_TXB1D7		0x4D
#define CAN_TXB2D0		0x56
#define CAN_TXB2D1		0x57
#define CAN_TXB2D2		0x58
#define CAN_TXB2D3		0x59
#define CAN_TXB2D4		0x5A
#define CAN_TXB2D5		0x5B
#define CAN_TXB2D6		0x5C
#define CAN_TXB2D7		0x5D

#define CAN_TX0IE		2
#define CAN_B0RTSM		0
#define CAN_RX0IF		0
#define CAN_RX1IF		1
#define CAN_RX0IE		0
#define CAN_RX1IE		1
#define CAN_B0BFM		0
#define CAN_B1BFM		1
#define CAN_B0BFE		2
#define CAN_B1BFE		3

// CAN controller modes
#define MODE_NORMAL			0b00000000
#define MODE_SLEEP 			0b00100000
#define MODE_LOOPBACK		0b01000000
#define MODE_LISTEN_ONLY  	0b01100000
#define MODE_CONFIG			0b10000000
#define MODE_MASK			0b11100000


// Functions
BOOL mcp_init();

void mcp_reset();

uint8_t mcp_read_byte(uint8_t address);

void mcp_write_byte(uint8_t address, uint8_t data);

void mcp_rts(uint8_t buffer);

void mcp_bit_modify(uint8_t address, uint8_t data, uint8_t mask);

uint8_t mcp_read_status(void);

uint8_t mcp_rx_status(void);
